package com.nuc.kl.module.basedata.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.nuc.kl.base.BaseAction;
import com.nuc.kl.module.basedata.entity.Direction;
import com.nuc.kl.module.basedata.entity.ReferenceType;

@Controller
@Scope("prototype")
public class ReferenceTypeAction extends BaseAction<ReferenceType>{
	
	private String ids;// 删除所用多个id
	private String findName;// 模糊查询所用

	private JSONObject result;// 返回的json
	private String rows;// 每页显示的记录数
	private String page;// 当前第几页
	
	public String getById() throws Exception{
		ReferenceType referenceType = referenceTypeService.getById(model.getId());
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().write(JSONArray.fromObject(referenceType).toString());	
		
		return null;
	}
	
	/** 列表 */
	public String listJson() throws Exception {
		// 当前页
		int intPage = Integer.parseInt((page == null || page == "0") ? "1"
				: page);
		// 每页显示条数
		int number = Integer.parseInt((rows == null || rows == "0") ? "10"
				: rows);
		// 每页的开始记录 第一页为1 第二页为number +1
		int start = (intPage - 1) * number;
		
		List<ReferenceType> list = null;
		
		if(findName == null){
			list = referenceTypeService.findAllPage(start, number);
		}else{
			list = referenceTypeService.findBySQL(start, number, findName);
		}

		Map<String, Object> jsonMap = new HashMap<String, Object>();// 定义map
		jsonMap.put("total", referenceTypeService.count(model));// total键 存放总记录数，必须的
		jsonMap.put("rows", list);// rows键 存放每页记录 list
		result = JSONObject.fromObject(jsonMap);// 格式化result 一定要是JSONObject

		return "listJson";
	}

	/** list转json  主要用于combo的显示 */
	public void listJsonArray(){
		models = referenceTypeService.findAll();
		try {
			toBeJsonArray(models, new String[0]);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/** 列表 */
	public String list() throws Exception {
		return "list";
	}

	/** 同时删除多个 */
	public String deleteByIds() throws Exception {
		String[] idList = ids.split(",");
		for (String str : idList) {
			referenceTypeService.delete(Long.parseLong(str));
		}
		return "toList";
	}

	/** 添加 */
	public String add() throws Exception {
		referenceTypeService.save(model);
		return "toList";
	}

	/** 修改 */
	public String edit() throws Exception {
		referenceTypeService.update(model);
		return "toList";
	}

	/**
	 * 模糊查询
	 * 
	 * @return
	 * @throws Exception
	 */
	public String findByNameLike() throws Exception {
		
		String sql = "FROM ReferenceType s WHERE s.name LIKE '%" + findName + "%'";
		
		List<ReferenceType> list = referenceTypeService.findBySQL(sql);
		
		Map<String, Object> jsonMap = new HashMap<String, Object>();// 定义map
		jsonMap.put("total", list.size());// total键 存放总记录数，必须的
		jsonMap.put("rows", list);// rows键 存放每页记录 list
		result = JSONObject.fromObject(jsonMap);// 格式化result 一定要是JSONObject

		return "listJson";
	}

	// -----------------getter-----------setter---------------------------

	public JSONObject getResult() {
		return result;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public void setResult(JSONObject result) {
		this.result = result;
	}

	public String getRows() {
		return rows;
	}

	public void setRows(String rows) {
		this.rows = rows;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public String getFindName() {
		return findName;
	}

	public void setFindName(String findName) {
		this.findName = findName;
	}
	
	
	
}
